package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.TimeAndCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class MemberReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements MemberReservationService {
    @Autowired
    private ReservationMapper reservationMapper;
    @Override
    public int getCancelledReservationCount(Long userId) {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Reservation::getStatus, 2)
                .eq(Reservation::getUpdateBy, userId)
                .between(Reservation::getTime, startTime, endTime);
        return (int) count(queryWrapper) ;
    }

    @Override
    public List<TimeAndCountVo> getCountByTime(Long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return reservationMapper.getCountByTime(startTime, endTime);
    }

    @Override
    public void addReservation(Reservation reservation) {
        reservation.setStatus(0);
        save(reservation);
    }

    @Override
    public TableDataInfo<Reservation> page(Integer pageSize, Integer pageNum) {
        Page<Reservation> page= new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select();
        page = page(page, queryWrapper);
        return bulideTableDate( page);
    }

    @Override
    public void cancelReservation(Long id) {
        // 先构建 LambdaUpdateWrapper
        LambdaUpdateWrapper<Reservation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(Reservation::getStatus, 2)
                .eq(Reservation::getId, id);
        update(updateWrapper);
    }

    @Override
    public void updateReservationStatus() {
        List<Reservation> reservations = reservationMapper.selectList(Wrappers.<Reservation>lambdaQuery()
                .lt(Reservation::getTime, LocalDateTime.now().minusMinutes(30))
                .eq(Reservation::getStatus, 0)
        );
        reservations.forEach(reservation -> {
            reservation.setStatus(2);
        });
        updateBatchById(reservations);
    }

    private TableDataInfo<Reservation> bulideTableDate(Page<Reservation> page) {
        TableDataInfo<Reservation> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
}
